<?php

namespace app\admin\server\circle;

use app\admin\ServerBase;
use app\admin\model\circle\CircleTypeModel;
use app\admin\validate\circle\CircleTypeVal;
use think\Request;

class CircleTypeServer extends ServerBase
{

    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new CircleTypeModel();
        $this->validate= new CircleTypeVal();
    }

    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        $list = $this->model->page($page,$limit)
            ->order('sort desc,id desc')
            ->select()->toArray();
        $result['data']=$list;
        $result['count']=$this->model->count();
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }
    function getDetail(){
        $info = $this->model->getFindById($this->request->param('id'));
        $this->getMessageCode('adminMessage.success.select',2);
        return $info;
    }
    function add(){
        $post = $this->request->post();
        if(!$this->validate->scene('create')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        if($this->model->save($post)){
            $this->getMessageCode('adminMessage.success.insert',2);
            return true;
        }else{
            $this->getMessageCode('adminMessage.error.insert',1);
            return false;
        }
    }
    function edit(){
        $id = $this->request->param('id',0);
        $post = $this->request->post();
        $post['id'] = $id;
        if(!$this->validate->scene('edit')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $info  = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
        if($info->save($post)){
            $this->getMessageCode('adminMessage.success.update',2);
            return true;
        }else{
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
    }
}